﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace WoW_Beast
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void savedVariablesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SavedVariables().Show();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            RegistryKey registry = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Blizzard Entertainment\World of Warcraft");
            if (registry != null)
            {
                Program.wowDir = Convert.ToString(registry.GetValue("InstallPath"));
                registry.Close();
            }

            while (!System.IO.File.Exists(Program.wowDir + "Wow.exe"))
            {
                MessageBox.Show("Wow.exe not found.");
                FolderBrowserDialog fdb = new FolderBrowserDialog();
                fdb.Description = "Select your World of Warcraft directory.";
                fdb.ShowDialog();
                Program.wowDir = fdb.SelectedPath;
            }
        }

        private void deleteCacheToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Delete(Program.wowDir + "Cache", true);
                MessageBox.Show("Cache deleted succesfully.");
            }
            catch
            { 
                MessageBox.Show("No Cache found."); 
            }
        }

        private void deletePatchInstallersToolStripMenuItem_Click(object sender, EventArgs e)
        {

            FileInfo[] patchFiles = new DirectoryInfo(Program.wowDir).GetFiles("WoW-*");

            if (patchFiles.Length == 0)
            {
                MessageBox.Show("No patch files found.");
                return;
            }

            String deleteMsg = "The following files will be deleted: \n";
            for (int i = 0; i <= patchFiles.Length - 1; i++)
            {
                if (i < 16)
                {
                    String item = patchFiles[i].Name;
                    deleteMsg += item + "\n";
                }
                else if (i == 16)
                {
                    deleteMsg += "...";
                }
            }
            DialogResult cont = MessageBox.Show(deleteMsg, "File Delete Confirmation", MessageBoxButtons.YesNo);
            if (cont == DialogResult.No)
                return;


            foreach (FileInfo file in patchFiles)
            {
                file.Delete();
            }
            if (Directory.Exists(Program.wowDir + "Updates"))
            {
                if (MessageBox.Show("Would you also like to delete the Updates folder?", "File Delete Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Directory.Delete(Program.wowDir + "Updates", true);
                }
            }
            MessageBox.Show("Patch installer deletion complete.");
        }

        private void deleteLogsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Delete(Program.wowDir + "Logs", true);
                MessageBox.Show("Successfully deleted Logs");
            }
            catch 
            {
                MessageBox.Show("No Logs found.");
            }
        }

        private void deleteErrorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Delete(Program.wowDir + "Errors", true);
                MessageBox.Show("Successfully deleted Errors");
            }
            catch
            {
                MessageBox.Show("No Errors found.");
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
